<?php
/*
	--------------------------------------------------------------
	GMMeta.php 2011-09-15 gm
	Gambio GmbH
	http://www.gambio.de
	Copyright (c) 2011 Gambio GmbH
   Released under the GNU General Public License (Version 2)
   [http://www.gnu.org/licenses/gpl-2.0.html]
	--------------------------------------------------------------

*/

	/*
	*	class GMMeta 	
	*/
	class GMMeta 
	{

		var $gm_lang_id;
		var $gm_lang_code;
		var $gm_language_charset;

		var $use_title;
		var $show_title;
		var $separator;
		var $title;

		/*
		*	class GMMeta 	
		*/
		function GMMeta() 
		{
			// get language
			if (!class_exists('language')) 
			{
				include_once(DIR_WS_CLASSES . 'language.php');
			}

			$lng = new language(xtc_input_validation('', 'char', ''));
			$lng->get_browser_language();
			
			if(!empty($_SESSION['languages_id']))
			{
				$this->gm_lang_id = $_SESSION['languages_id'];
			}
			else
			{
				$this->gm_lang_id = $lng->language['id'];
			}

			$this->gm_lang_code					= $lng->language['code'];
			$this->gm_language_charset			= $lng->language['language_charset'];

			$this->use_standard_title			= gm_get_conf('GM_TITLE_USE_STANDARD_META_TITLE');
			$this->show_standard_title			= gm_get_conf('GM_TITLE_SHOW_STANDARD_META_TITLE');
			$this->standard_title_separator	= gm_get_content('GM_TITLE_STANDARD_META_TITLE_SEPARATOR',	$this->gm_lang_id);
			
			$t_standard_title = gm_get_content('GM_TITLE_STANDARD_META_TITLE', $this->gm_lang_id);		
			if(empty($t_standard_title))
			{
				$t_standard_title = htmlspecialchars(STORE_NAME);
			}
			else
			{
				$t_standard_title = htmlspecialchars($t_standard_title);
			}			
			$this->standard_title				= $t_standard_title;

			return;
		}			

		/*
		*	get metatags 	
		*/
		function get($cPath, $product) 
		{			
			// get essential meta 
			$this->get_base_meta();
			
			// get optional metatags
			$this->get_optional_meta($cPath, $product);			

			// get content metatags
			$this->get_content_meta($cPath, $product);

			return;
		}		

		/*
		*	get essential metatags
		*/
		function get_base_meta()
		{
			echo '<meta http-equiv="Content-Type" content="text/html; charset=' . $this->gm_language_charset . '" />'	. "\n";
			echo '<meta http-equiv="Content-Style-Type" content="text/css" />'											. "\n";
			echo '<meta name="robots" content="' . gm_get_content('robots', $this->gm_lang_id) . '" />'					. "\n";
			echo '<meta name="language" content="' . $this->gm_lang_code . '" />'										. "\n";
			
			return;
		}		
		
		/*
		*	get metatags generated by content	
		*/
		function draw_page_title($content, $optional='') 
		{	
			if(!empty($_GET['page'])) 
			{
				$page = ' ' . GM_PAGE . ' ' . $_GET['page'];
			}
			
			if(!empty($content)) 
			{				
				$title .= htmlspecialchars($content)	.	$page . "\n";
			} 
			else 
			{
				$title .= htmlspecialchars($optional)	.	$page . "\n";
			}	
			
			if(empty($title))
			{
				$title = htmlspecialchars(STORE_NAME);
			}
			else
			{
				if((int)$this->use_standard_title == 1)
				{
					if($this->show_standard_title == 'before' || empty($this->show_standard_title))
					{
						$title = $this->standard_title . $this->standard_title_separator . $title;
					}
					else if($this->show_standard_title == 'after')
					{
						$title = $title . $this->standard_title_separator . $this->standard_title;
					}
				}
			}

			echo '<title>' . trim($title) . '</title>' . "\n";

			return;
		}	

		/*
		*	get metatags generated by content	
		*/
		function draw_meta_tag($name, $content, $optional="") 
		{			
			if(empty($content) && !empty($optional)) 
			{
				$content = $optional;
			}

			if(!empty($content)) 
			{				
				echo '<meta name="' . $name . '" content="' . htmlspecialchars($content) . '" />' . "\n";
			} 	

			return;
		}	

		/*
		*	get metatags generated by content	
		*/
		function get_content_meta($cPath, $product) 
		{			
			$gm_default = gm_get_content(array('keywords'), $this->gm_lang_id);			
			
			// handle products
			if($product->pID !=0) 
			{		
				$this->draw_meta_tag('keywords',	$product->data['products_meta_keywords'],		gm_get_content('keywords', $this->gm_lang_id));			
				$this->draw_meta_tag('description', $product->data['products_meta_description'],	gm_get_content('description', $this->gm_lang_id));				
				$this->draw_page_title($product->data['products_meta_title'], $product->data['products_name']);			

			// handle categories
			} 
			else if(!empty($cPath)) 
			{
				$gm_cat_id = explode('_', $cPath);

				$categories_meta_query = xtc_db_query("
													SELECT
														categories_meta_keywords,
														categories_meta_description,
														categories_meta_title,
														categories_name
													FROM " . 
														TABLE_CATEGORIES_DESCRIPTION . "
													WHERE 
														categories_id='" . xtc_db_input(end($gm_cat_id)) . "'
													AND
														language_id='" . $this->gm_lang_id . "'
													
													");

				$categories_meta = xtc_db_fetch_array($categories_meta_query, true);

				$this->draw_meta_tag('keywords',	$categories_meta['categories_meta_keywords'],		gm_get_content('keywords', $this->gm_lang_id));			
				$this->draw_meta_tag('description', $categories_meta['categories_meta_description'],	gm_get_content('description', $this->gm_lang_id));				
				$this->draw_page_title($categories_meta['categories_meta_title'], $categories_meta['categories_name']);
				
			// handle contents
			} 
			else if(strpos(gm_get_env_info('SCRIPT_NAME'), 'shop_content.php') !== false || (strpos(gm_get_env_info('SCRIPT_NAME'), 'shop_content.php') === false && !is_object($cPath) && empty($product->pID)))
			{
				if(!empty($_GET['manufacturers_id'])) 
				{
					$manu_query = xtc_db_query("
														SELECT 
															manufacturers_name
														AS
															name
														FROM 
															manufacturers
														WHERE 
															manufacturers_id='" . xtc_db_input($_GET['manufacturers_id']) . "'
														");

					$manu_meta = xtc_db_fetch_array($manu_query, true);

					$this->draw_page_title($manu_meta['name']);

				} 
				else 
				{					
					$gm_coID = (int)$_GET['coID'];

					if(strpos(strtolower(gm_get_env_info('SCRIPT_NAME')), 'index.php') !== false && empty($gm_coID))
					{
						$gm_coID = 5;
					}

					$contents_meta_query = xtc_db_query("
														SELECT 
															content_title,
															content_heading,
															contents_meta_title,
															contents_meta_keywords,
															contents_meta_description
														FROM " . 
															TABLE_CONTENT_MANAGER . "
														WHERE 
															content_group='" . xtc_db_input($gm_coID) . "'
														AND
															languages_id='" . $this->gm_lang_id . "'
														");

					$contents_meta = xtc_db_fetch_array($contents_meta_query, true);
					
					if(empty($contents_meta['content_heading'])) 
					{
						$contents_meta['content_heading'] = $contents_meta['content_title'];
					}
                   
					$this->draw_page_title($contents_meta['contents_meta_title'], $contents_meta['content_title']);
					$this->draw_meta_tag('keywords',	$contents_meta['contents_meta_keywords'],		gm_get_content('keywords',		$this->gm_lang_id));	
					$this->draw_meta_tag('description', $contents_meta['contents_meta_description'],	gm_get_content('description',	$this->gm_lang_id));
				}
			} 
		}			

		/*
		*  get all optional metatags
		*/
		function get_optional_meta($cPath, $product) 
		{		
			$gm_metatags = gm_get_content_by_group_id(1, $this->gm_lang_id);
			foreach($gm_metatags as $gm_key => $gm_value) 
			{			
				if($gm_key != 'keywords' && $gm_key != 'robots') 
				{		
					if((!empty($cPath) && $gm_key == 'description') || ($product->pID !=0 && $gm_key == 'description') || (isset($_GET['coID']) && $gm_key == 'description')
						|| (strpos(gm_get_env_info('SCRIPT_NAME'), 'shop_content.php') === false && !is_object($cPath) && empty($product->pID) && $gm_key == 'description'))
					{
						
					} 
					else 
					{
						echo '<meta name="' . $gm_key . '" content="' . htmlspecialchars($gm_value) . '" />' . "\n";
					}
				}
			}
			return;
		}
	}
?>